const XLSX = require('xlsx');
import { saveAs } from 'file-saver';
function onExportExcel(fileList, headerMapping, fileName) {
  let fileListTemp = fileList;

  // 提取并转化表头
  const header = Object.keys(headerMapping).map((key) => headerMapping[key]);
  const content = fileListTemp.map(item => Object.keys(headerMapping).map(key => item[key]));
  const excelData = [header, ...content];

  const ws = XLSX.utils.aoa_to_sheet(excelData);
  const wb = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
  XLSX.writeFile(wb, `${fileName}.xlsx`);
  saveAs(new Blob([wb]), `${fileName}.xlsx`);
}

export default onExportExcel;